﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MachineBuildingSystem
{
    public partial class WorkingConfigForm : Form
    {
        //选择的工作中心
        private long m_SelectedWokingId = -1;
        public long SelectedWokingId
        {
            get { return m_SelectedWokingId; }
            set { m_SelectedWokingId = value; }
        }

        private string m_SelectedWoking = "";
        public string SelectedWoking
        {
            get { return m_SelectedWoking; }
            set { m_SelectedWoking = value; }
        }

        private long m_OrderId = -1;
        public long OrderId
        {
            set { m_OrderId = value; }
        }

        public WorkingConfigForm()
        {
            InitializeComponent();
        }

        private void WorkingConfigForm_Load(object sender, EventArgs e)
        {
            InitMruEditWorking();
            if (m_SelectedWokingId == -1)
                return;
            mruEditWorking.SelectedItem = FindDataInfoByKey(mruEditWorking, m_SelectedWokingId);
        }

        //初始化生产线下拉框
        private void InitMruEditWorking()
        {
            try
            {
                string str = ConfigurationManager.AppSettings["ProductLines"].ToString();
                string[] productLineArr = str.Split(',');
                if (productLineArr.Length == 0)
                    return;
                mruEditWorking.Properties.Items.Clear();
                foreach (string productLine in productLineArr)
                {
                    string[] strArr = productLine.Split('/');
                    DataInfo data = new DataInfo();
                    data.Id = Convert.ToInt64(strArr[0]);
                    data.Value = strArr[1];
                    mruEditWorking.Properties.Items.Add(data);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("初始化生产线下拉框数据异常：" + ex.Message);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (m_SelectedWokingId == -1)
            {
                MessageBox.Show("请选择生产线！");
                return;
            }
            m_SelectedWoking = this.mruEditWorking.Text.Trim();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        //根据编码获取DataInfo
        public static DataInfo FindDataInfoByKey(MRUEdit cmb, long key)
        {
            foreach (DataInfo info in cmb.Properties.Items)
            {
                if (info.Id == key)
                {
                    return info;
                }
            }
            return null;
        }

        private void mruEditWorking_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mruEditWorking.SelectedItem == null
                || mruEditWorking.SelectedItem.ToString() == ""
                || !(mruEditWorking.SelectedItem is DataInfo))
                m_SelectedWokingId = 0;
            else
                m_SelectedWokingId = ((DataInfo)mruEditWorking.SelectedItem).Id;
        }
    }
}
